/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.DTO.OutputFinancialFactorDTO;
import cn.edu.nju.software.greencreditsystem.domain.FinancialFactor;
import cn.edu.nju.software.greencreditsystem.domain.FinancialScore;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.FinancialService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-19
 * 类描述：银行财务模块的action封装类
 */
public class EnterpriseFinancialAction extends ActionSupport implements ServletContextAware {
    private int enterpriseId;//企业的id
    private int factorId;//财务指标的id
    private List<FinancialFactor> factorList;// 所有财务因素的指标
    private List<OutputFinancialFactorDTO> factorDTOList;//财务因素的前台显示list
    private OutputFinancialFactorDTO factorDTO;//某个具体财务指标的前台显示DTO
    private List<FinancialScore> factorHistory;//某个具体财务指标的历史记录
    private BufferedImage image;//某个财务指标的折线图
    private File file;//临时图片文件
    private ServletContext context;
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute(){
        return SUCCESS;
    }
    
    /**
     * 方法描述：企业跳转至查看财务主界面
     * param：
     * return：SUCCESS
     */
    public String lookup_Financial(){
        //取得请求相关的ActionContext实例  
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        enterpriseId = Integer.valueOf((String) session.get("userId"));
        
        //取得所有的财务因素
        FinancialService fs = ServiceFactory.getFinancialServiceInstance();
        this.factorList = fs.get_AllFinancialFactor();
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：企业查看所有财务因素的概况
     * param：
     * return：SUCCESS
     */
    public String lookup_All(){
        //取得全部财务因素的概况(衡量数值)
        FinancialService fs = ServiceFactory.getFinancialServiceInstance();
        this.factorDTOList = fs.lookup_AllFinancialFactor(enterpriseId);
        
        return SUCCESS;
    }
    
    
    /**
     * 方法描述：企业查看某个特定因素的具体情况
     * param：
     * return：SUCCESS
     */
    public String lookup_Factor(){
        FinancialService fs = ServiceFactory.getFinancialServiceInstance();
        this.factorDTO = fs.lookup_A_FinancialFactor(enterpriseId, factorId);//取得该指标的概况
        this.factorHistory = fs.get_FactorHistory(enterpriseId, factorId);
        this.image = fs.get_FactorImage(enterpriseId, factorId);
        //生成一个临时图片以供图片显示(先删除上次生成的临时图片)
        if(file != null){
            file.delete();
        }else{
            file = new File(context.getRealPath("/enterprise/image"), "temp"+enterpriseId+".png");
        }
        try {
            ImageIO.write(image, "png", file);
            image.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        return SUCCESS;
    }
    
    
    
    

    public List<FinancialFactor> getFactorList() {
        return factorList;
    }

    public void setFactorList(List<FinancialFactor> factorList) {
        this.factorList = factorList;
    }

    public List<OutputFinancialFactorDTO> getFactorDTOList() {
        for(int i=0; i<factorDTOList.size(); i++){
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
            factorDTOList.get(i).setLatestValue(Double.parseDouble(df.format(factorDTOList.get(i).getLatestValue()).replace(",", "")));
            factorDTOList.get(i).setMaxValue(Double.parseDouble(df.format(factorDTOList.get(i).getMaxValue()).replace(",", "")));
            factorDTOList.get(i).setMinValue(Double.parseDouble(df.format(factorDTOList.get(i).getMinValue()).replace(",", "")));
            factorDTOList.get(i).setUpdownMargin(Double.parseDouble(df.format(factorDTOList.get(i).getUpdownMargin()).replace(",", "")));
            factorDTOList.get(i).setUpdownValue(Double.parseDouble(df.format(factorDTOList.get(i).getUpdownValue()).replace(",", "")));
        }
        return factorDTOList;
    }

    public void setFactorDTOList(List<OutputFinancialFactorDTO> factorDTOList) {
        this.factorDTOList = factorDTOList;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public OutputFinancialFactorDTO getFactorDTO() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        factorDTO.setLatestValue(Double.parseDouble(df.format(factorDTO.getLatestValue()).replace(",", "")));
        factorDTO.setMaxValue(Double.parseDouble(df.format(factorDTO.getMaxValue()).replace(",", "")));
        factorDTO.setMinValue(Double.parseDouble(df.format(factorDTO.getMinValue()).replace(",", "")));
        factorDTO.setUpdownMargin(Double.parseDouble(df.format(factorDTO.getUpdownMargin()).replace(",", "")));
        factorDTO.setUpdownValue(Double.parseDouble(df.format(factorDTO.getUpdownValue()).replace(",", "")));
        return factorDTO;
    }

    public void setFactorDTO(OutputFinancialFactorDTO factorDTO) {
        this.factorDTO = factorDTO;
    }

    public int getFactorId() {
        return factorId;
    }

    public void setFactorId(int factorId) {
        this.factorId = factorId;
    }

    public List<FinancialScore> getFactorHistory() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(4);
        for(int i=0; i<factorHistory.size(); i++){
            factorHistory.get(i).setValue(Double.parseDouble(df.format(factorHistory.get(i).getValue()).replace(",", "")));
        }
        return factorHistory;
    }

    public void setFactorHistory(List<FinancialScore> factorHistory) {
        this.factorHistory = factorHistory;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    @Override
    public void setServletContext(ServletContext sc) {
        this.context = sc;
    }

    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }
    
}
